
				
			import com.utils.BasicService;
			import com.views.SelectBookTitleWindow;
			
			import flash.events.MouseEvent;
			import flash.geom.Rectangle;
			
			import models.Sewar;
			
			import mx.collections.ArrayCollection;
			import mx.containers.Canvas;
			import mx.controls.Alert;
			import mx.core.Application;
			import mx.managers.PopUpManager;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.utils.ArrayUtil;
			import mx.utils.ObjectUtil;
			
			 
			
			private var drawStarted:Boolean=false;
			private var _isBooksLoaded:Boolean = false;
			private var startX:int;
			private var startY:int;
			private var rectangleTopLeftX:int;
			private var rectangleTopLeftY:int;
			private var rectangleWidth:int;
			private var rectangleHieght:int;
			private var rectangleShape:Canvas;
			[Bindable]
			private var counter=0;			
			
			[Bindable]
			private var _pageNumber:int=1;
			
			private var _bookId:int = -1;
			private var _numberOfPages = 0;
			
			private var _backEndURL:String = "http://localhost:3007/";
			
			private static const RECT_COLOR = 0xFFCC00;
			private static const RECT_ALPHA = .3;
			
			
			
			
            private var effects: Array ;
			
			[Bindable]
			public var books:Array;
			
			private var bookmarksCanvacesArray:Array;		
			
			private function init():void
			{
				application.addEventListener(KeyboardEvent.KEY_DOWN,escapeButtonPressed);
				displaySelectBookWindow();
				
				for(var i:int=0; i < sowarArr.length; i++)
				{
					sowarArrCollection.addItem(new Sewar(sowarArr[i].label, sowarArr[i].data));
				}
			
			}
			
			
			
			//opens the "select book" title window
			public function displaySelectBookWindow():void
			{	
				var booksWindow:SelectBookTitleWindow=SelectBookTitleWindow(PopUpManager.createPopUp( Application.application.mainCanvas, SelectBookTitleWindow , true));
				PopUpManager.centerPopUp(booksWindow);
			}
			
			
					
			/**
			 *  Start/Stop drawing the selection rectangle
			 */ 
			private function toggleDraw(event:MouseEvent)
			{
				if(drawStarted)
				{	
					//finish drawing the rectangle
					drawStarted=false;
				}else
				{
					//start drawing the rectangle 
					rectangleShape= new Canvas();
					rectangleShape.addEventListener(MouseEvent.CLICK,toggleDraw);
					mainCanvas.addChild(rectangleShape);
					bookmarksCanvacesArray[bookmarksCanvacesArray.length]=rectangleShape;
					startX =event.localX;
					startY = event.localY;
					drawStarted = true;
				}
			}
			
			
			// update drawing the selection rectangle
			private function updateDraw(event:MouseEvent)
			{
				if(drawStarted)
				{
					rectangleShape.graphics.clear();
					var endX: int = event.localX;
					var endY: int = event.localY;
					drawRectanlge(rectangleShape.graphics,startX,startY,endX,endY);					 
				}
			}
			
			
			private function escapeButtonPressed(event:KeyboardEvent):void
			{
				if (drawStarted)				
					if (event.keyCode == 27)   //escapse button	
					{
						drawStarted=false;
				 		if(rectangleShape!=null)
							rectangleShape.graphics.clear();
					}
				 		
			}
			
			
			//given start point and end point, it calculates the coordinates of the topLeft point
			// and the width and height of the rectangle 
			private function drawRectanlge(graphics:Graphics,startX:int,startY:int,endX:int,endY:int)
			{
				if(startX < endX)
				{
					rectangleTopLeftX = startX;
					var bottomRightX:int = endX;				
				}else
				{
					rectangleTopLeftX = endX;
					bottomRightX = startX;
				}
				if(startY < endY)
				{
				 	rectangleTopLeftY = startY;
					var bottomRightY:int = endY;				
				}else
				{
					rectangleTopLeftY = endY;
					bottomRightY =startY ;
				}
				
				graphics.beginFill(RECT_COLOR,RECT_ALPHA);
				rectangleWidth=bottomRightX-rectangleTopLeftX;
				rectangleHieght= bottomRightY-rectangleTopLeftY;
				graphics.drawRect(rectangleTopLeftX,rectangleTopLeftY,rectangleWidth,rectangleHieght);
			}					 
	
			
		
			
			private function nextPage():void
			{				
				if (_bookId < 0 ) return ; // no book is selected
				if (_pageNumber >= _numberOfPages)	return;
				
				cleanPage();
				
				_pageNumber ++;
				pageImage.source = getImageSource(_bookId,_pageNumber);
				
			}
			
			private function previousePage():void
			{
				if (_bookId < 0 ) return ; // no book is selected
				if (_pageNumber <=1) return;
				
				cleanPage();
				
				_pageNumber --;
				pageImage.source = getImageSource(_bookId,_pageNumber);
				
			}
			
			
			
			private function goToPage():void
			{
				if (_bookId < 0 ) return ; // no book is selected
				if ((Number(txtPageNumber.text)<1 ) || (Number(txtPageNumber)>_numberOfPages) )
					return; 
				
				cleanPage();
				
				_pageNumber = Number(txtPageNumber.text);
				pageImage.source = getImageSource(_bookId,_pageNumber);
				
			}
			
			
			public function getImageSource(bookId:int,pageNumber:int):String
			{
				var imageSource:String = Application.application.backEndURL+"books/" + bookId;
				imageSource += "/pages/"
				imageSource += pageNumber; 
				imageSource += ".jpg";
				return imageSource;
			}

			
			
						
			//remove all the effects and links from the current page
			public function cleanPage():void
			{
				for each (var bookmarkCanvas in bookmarksCanvacesArray)
				{
					mainCanvas.removeChild(bookmarkCanvas)
				}
				bookmarksCanvacesArray= null;	
			}
					
			
				
		 
		 private function createYellowRectangularCanvas(rectangle:Rectangle):Canvas
		 {
		 	var canvas:Canvas= new Canvas();		 	
		 	//mainCanvas.addChild(canvas);
		 	canvas.graphics.beginFill(RECT_COLOR,RECT_ALPHA);
		 	canvas.graphics.drawRect(0,0,rectangle.width,rectangle.height);
		 	canvas.graphics.endFill();
			canvas.x= rectangle.x;
			canvas.y= rectangle.y;
			return canvas;
		 }
			
			
		private function cut():void
		{
			//send to the backend
			//rectangle[x,y,width,height],filename
			/* rectangleTopLeftX/pageImage.width
			rectangleTopLeftY/pageImage.height
			rectangleWidth/pageImage.width
			rectangleHieght/pageImage.height  */
		
			var url:String = _backEndURL + "books/";
			url += _bookId ;
			url += "/pages/";
			url += _pageNumber;
			url += "/bookmarks.xml";
			trace(url);
			var service:BasicService= new BasicService(url,"POST",true,cutResult,cutFault);
			var paramters:Object = new Object();
			paramters["bookmark[bounds][x]"] = rectangleTopLeftX/pageImage.width;
			paramters["bookmark[bounds][y]"] = rectangleTopLeftY/pageImage.height;
			paramters["bookmark[bounds][width]"]= rectangleWidth/pageImage.width;
			paramters["bookmark[bounds][height]"]= rectangleHieght/pageImage.height;
	        paramters["bookmark[description]"] = lstSowar.selectedItem.data;
	        service.send(paramters);	
			
					
			Alert.show(lstSowar.selectedItem.data);
			counter++;
		}
		
		private function cutResult(event:ResultEvent):void
		{
			trace (event.result);
		}
		
		
		private function cutFault(event:FaultEvent):void
		{	
			trace (event.fault.faultString);
		}
		
		private function addSwar():void
		{
			var url:String = _backEndURL + "books/";
			url += _bookId ;
			url += "/pages/";
			url += _pageNumber;
			url += "/bookmarks.xml";
			trace(url);
			var service:BasicService= new BasicService(url,"POST",true,cutResult,cutFault);
			var paramters:Object = new Object();
			paramters["bookmark[bounds][x]"] = rectangleTopLeftX/pageImage.width;
			paramters["bookmark[bounds][y]"] = rectangleTopLeftY/pageImage.height;
			paramters["bookmark[bounds][width]"]= rectangleWidth/pageImage.width;
			paramters["bookmark[bounds][height]"]= rectangleHieght/pageImage.height;
	        paramters["bookmark[description]"] = lstSowar.selectedItem.data;
	        service.send(paramters);	
		}
		
		
		private function loadPageBookmarks():void
		{
			var url:String = _backEndURL + "books/";
			url += _bookId ;
			url += "/pages/";
			url += _pageNumber;
			url += "/bookmarks.xml";
			trace(url);
			var service:BasicService= new BasicService(url,"GET",true,bookmarksReceived);
		}	
		
		private function bookmarksReceived(event:ResultEvent):void
		{
			trace (ObjectUtil.toString(event.result));
			var bookmarks:Array;
			if (event.result.bookmarks)
			{
				if(event.result.bookmarks.bookmark is ArrayCollection) 
				{
			 		bookmarks = event.result.bookmarks.bookmark.source;
			 	}
			 	else 
			 	{
			 		bookmarks = ArrayUtil.toArray(event.result.bookmarks.bookmark);
			 	} 
			}
			else
				bookmarks = null;
			
			for each (var bookmark:Object in bookmarks)
			{
				var rectangle:Rectangle = new Rectangle(bookmark.x.value,bookmark.y.value,bookmark.width.value,bookmark.height.value);
				var bookmarkCanvas:Canvas= createYellowRectangularCanvas(rectangle);
				bookmarksCanvacesArray[bookmarksCanvacesArray.length]=bookmarkCanvas;
				mainCanvas.addChild(bookmarkCanvas);
			}
			
		}
			
		//////////////////////////////// getters and setters ///////////////////////////////////////////////
		public function get isBooksLoaded():Boolean
		{
			return _isBooksLoaded;
		}
		
		
		public function set isBooksLoaded(value:Boolean):void
		{
			_isBooksLoaded= value;
		}
		
		
		
		public function get bookId():int
		{
			return _bookId;
		}
		
		
		public function set bookId(value:int):void
		{
			_bookId=value;
		}
		
		public function get pageNumber():int
		{
			return _pageNumber;
		}
		
		public function set pageNumber(value:int)
		{
			_pageNumber=value;
		}
		
		
		public function get numberOfPages():int
		{
			return _numberOfPages;
		}
		
		
		public function set numberOfPages(value:int):void
		{
			_numberOfPages=value;
		}
						
				
		public function get backEndURL():String
		{
			return _backEndURL;
		}
		
		public function set backEndURL(value:String):void
		{
			_backEndURL = value;
		}
		